package com.qingxin.park.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.qingxin.park.entity.Crops;
import com.qingxin.park.entity.CropsType;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.ui.bean.TreeNode;


/**
 * 农作物分类
 * @author Administrator
 *
 */
@Service
public class CropsTypeService extends BaseService<CropsType, String> {
	
	
	/**
	 * 获取菜单树数据
	 * @param parkid
	 * @param isParent
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TreeNode> getUITreeData(String parkid, boolean isParent){
		List<TreeNode> list = new ArrayList<TreeNode>();
		StringBuilder hql = new StringBuilder();
		hql.append("select new com.qingxin.park.entity.CropsType(p.typeid, p.typeName)");
		hql.append(" from CropsType p where p.pid='0'");
		hql.append(" order by p.typeName asc");
		List<CropsType> types = listByHql(hql.toString());
		if(types != null){
			for(CropsType t : types){
				list.add(new TreeNode(t.getTypeid(), t.getTypeName(), "TYPE", null, isParent));
			}
		}
		return list;
	}
	
	/**
	 * 列表数据
	 * @param parkid
	 * @param typeid
	 * @param cropName
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	public Pager list(PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("select new com.qingxin.park.entity.CropsType(" +
				"t.typeid,t.pid, t.typeName,t.typeCode,t.remark)" +
				" from CropsType t ");
		
		querier.append(pageParam.getOrderby(" order by t.typeName asc", "t"));
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	/**
	 * 添加大类信息
	 * @param cropsType
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(CropsType cropsType, LoginUser loginUser, HttpServletRequest request){
		cropsType.setPid("0");
		this.save(cropsType);
		return null;
	}
	
	/**
	 * 修改大类信息
	 * @param cropsType
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(CropsType cropsType, LoginUser loginUser, HttpServletRequest request){
		CropsType old = getSafeEntity(cropsType.getTypeid());
		if(old == null){
			return new SysError("参数错误");
		}
		
		String fields = "typeName,typeCode,remark";
		BeanUtil.setTargetFields(old, cropsType, fields, true);
		
		Updater<CropsType> updater = new Updater<>(old, fields);
		this.update(updater);
		
		return null;
		
	}
	
	/**
	 * 删除大类信息
	 * @param typeids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] typeids, LoginUser loginUser, HttpServletRequest request){
		for(String typeid : typeids){
			CropsType cropsType = get(typeid);
			if(cropsType==null){
				continue;
			}
			
			this.delete(cropsType);
		}
		
		return null;
	}
	
	
	
}
